/**
 * Created by Rubekid on 2016/11/8.
 */
/**
 * 模态窗口
 */
window.Modal = {
    tpls:{
        alert:'<div class="modal fade" tabindex="-1" role="dialog"><div class="modal-dialog modal-sm dialog-modal"  role="document"><div class="modal-content"><div class="modal-body"><p class="message-box text-center"></p></div><div class="modal-footer"><button type="button" class="btn btn-primary" data-dismiss="modal">确定</button></div></div></div></div>',
        confirm:'<div class="modal fade"><div class="modal-dialog modal-sm  dialog-modal" role="document"><div class="modal-content"><div class="modal-body"><p class="message-box  text-center"></p></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">取消</button><button type="button" class="btn btn-primary">确定</button></div></div></div></div>',
        loading:'<div class="modal fade"><div class="modal-dialog modal-sm  dialog-modal" role="document"><div class="modal-content" style="box-shadow: none;border: none;background: rgba(0,0,0,0.5); color: #FFFFFF;"><div class="modal-body"><div class="text-center"><i class="fa fa-spinner fa-spin fa-2x"></i></div> <div class="message-box text-center form-control-static">正在载入...</div></div></div></div></div>',
        toast:'<div class="modal fade toast"><div class="modal-dialog modal-sm  dialog-modal" role="document"><div class="modal-content" style="box-shadow: none;border: none;background: rgba(0,0,0,0.5); color: #FFFFFF;"><div class="modal-body"><div class="message-box text-center form-control-static"></div></div></div></div></div>'
    },
    register:function(events){
        events = events || [];
        if(events.length == 0){
            events = ['alert', 'confirm', 'loading', 'toast'];
        }
        var body = $(document.body);
        this.modal = {};
        for(var i=0; i<events.length; i++){
            var event = events[i];
            var tpl = this.tpls[event];
            if(tpl){
                this.modal[event] = $(tpl);
                var config = {show:false};
                if(event == "loading" || event== "toast"){
                    config.backdrop = 'static';
                }
                this.modal[event].modal(config);
                body.append(this.modal[event]);
            }
        }
    },
    alert:function(message, config){
        config = config || {};
        var confirmText = config.confirmText || "确定";
        var title = config.title || "系统提示";
        this.modal.alert.find(".modal-title").text(title);
        this.modal.alert.find(".btn-primary").text(confirmText);
        this.modal.alert.find(".message-box").html(message);
        this.modal.alert.modal("show");
    },
    confirm:function(message, config){
        config = config || {};
        var confirmText = config.confirmText || "确定";
        var cancelText = config.cancelText || "取消";
        var title = config.title || "系统提示";
        this.modal.confirm.find(".modal-title").text(title);
        this.modal.confirm.find(".btn-primary").text(confirmText);
        this.modal.confirm.find(".btn-default").text(cancelText);
        this.modal.confirm.find(".message-box").html(message);
        if(config.notTitle){
            this.modal.confirm.find(".modal-header").hide();
        }
        else{
            this.modal.confirm.find(".modal-header").show();
        }
        this.modal.confirm.modal("show");
        var _this = this;
        this.modal.confirm.find(".btn-primary").off("click").on("click", function(){
            if(typeof config.confirm == "function"){
                config.confirm();
            }
            _this.modal.confirm.modal("hide");
        });

    },
    loading:function(message){
        if(this.modal.loading.showing){
            return ;
        }
        this.modal.loading.find(".message-box").html(message||"加载中…");
        this.modal.loading.modal("show");
        this.modal.loading.showing = true;
    },
    dismissLoading:function(){
        var _this = this;
        this.modal.loading.find(".message-box").html("");
        this.modal.loading.modal("hide");
        this.modal.loading.on("hidden.bs.modal", function(){
            _this.modal.loading.showing = false;
        });
    },
    toast:function(message){
        if(this.toastHideTimeout){
            clearTimeout(this.toastHideTimeout);
        }
        this.modal.toast.find(".message-box").html(message);
        this.modal.toast.modal("show");
        var _this = this;
        this.toastHideTimeout = setTimeout(function(){
            _this.modal.toast.modal("hide");
            _this.toastHideTimeout = null;
        }, 1500);

    },
    success:function(message){
        this.toast(message);
    },
    error:function(message){
        this.toast(message);
    }
};
Modal.register();